//
// MethodReference.cs
//
// Author:
//   Jb Evain (jbevain@gmail.com)
//
// (C) 2005 - 2008  Jb Evain
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Text;

namespace Mono.Cecil {

	public class MethodReference : MemberReference, IMethodSignature, IGenericParameterProvider, IGenericContext {

		ParameterDefinitionCollection parameters;
		MethodReturnType return_type;

		bool has_this;
		bool explicit_this;
		MethodCallingConvention calling_convention;
		internal GenericParameterCollection generic_parameters;

		public virtual bool HasThis {
			get { return has_this; }
			set { has_this = value; }
		}

		public virtual bool ExplicitThis {
			get { return explicit_this; }
			set { explicit_this = value; }
		}

		public virtual MethodCallingConvention CallingConvention {
			get { return calling_convention; }
			set { calling_convention = value; }
		}

		public bool HasParameters {
			get { return parameters != null && parameters.Count > 0; }
		}

		public virtual ParameterDefinitionCollection Parameters {
			get {
				if (parameters == null)
					parameters = new ParameterDefinitionCollection ();

				return parameters;
			}
		}

		IGenericParameterProvider IGenericContext.Type {
			get { return DeclaringType; }
		}

		IGenericParameterProvider IGenericContext.Method {
			get { return this; }
		}

		GenericParameterType IGenericParameterProvider.GenericParameterType {
			get { return GenericParameterType.Method; }
		}

		public virtual bool HasGenericParameters {
			get {
				return generic_parameters != null && generic_parameters.Count > 0;
			}
		}

		public virtual GenericParameterCollection GenericParameters {
			get {
				if (generic_parameters != null)
					return generic_parameters;

				return generic_parameters = new GenericParameterCollection ();
			}
		}

		public TypeReference ReturnType {
			get {
				var return_type = MethodReturnType;
				return return_type != null ? return_type.ReturnType : null;
			}
		}

		public virtual MethodReturnType MethodReturnType {
			get { return return_type; }
			set { return_type = value; }
		}

		public override string FullName {
			get {
				var builder = new StringBuilder ();
				builder.Append (ReturnType.FullName);
				builder.Append (" ");
				builder.Append (MemberFullName ());
				builder.Append ("(");
				if (HasParameters) {
					var parameters = Parameters;
					for (int i = 0; i < parameters.Count; i++) {
						var parameter = parameters [i];
						if (i > 0)
							builder.Append (",");

						if (parameter.ParameterType.IsSentinel)
							builder.Append ("...,");

						builder.Append (parameter.ParameterType.FullName);
					}
				}
				builder.Append (")");
				return builder.ToString ();
			}
		}

		public virtual bool IsGenericInstance {
			get { return false; }
		}

		public virtual bool IsDefinition {
			get { return false; }
		}

		internal MethodReference ()
			: base (null)
		{
			return_type = new MethodReturnType (null);
		}

		public virtual MethodReference GetElementMethod ()
		{
			return this;
		}

		public virtual MethodDefinition Resolve ()
		{
			var declaring_type = this.DeclaringType;

			if (declaring_type == null || declaring_type.Module == null)
				throw new NotSupportedException ();

			return declaring_type.Module.Resolve (this);
		}
	}
}
